package com.interview.javabase.algorithm;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 二分搜索
 * @author: zhangshuguang@vichain.com
 * @date: 2019/07/24 11:19
 */
public class BinarySearch {

    public static Boolean search(List<Integer> needSearchList, Integer value) {

        int low = 0;
        int high = needSearchList.size() - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            Integer curValue = needSearchList.get(mid);
            if (value > curValue) {
                low = mid + 1;
            } else if (value.equals(curValue)) {
                return true;
            } else {
                high = mid - 1;
            }
        }
        return false;
    }

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>(){{
            add(11);
            add(23);
            add(42);
            add(345);
            add(151);
            add(567);
            add(4564);
            add(454);
        }};
        Boolean search = search(list, 567);
        System.out.println(search);
    }
}
